<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;
use think\Request;

class Power extends Admincontroller
{
    public function list()
    {
        //获取用户表信息
        $list = Db::name('powers')->field(true)->order('id','ASC')->select();
        return $this->fetch('power/list',[
            'list' => $list
        ]);
    }

    public function add()
    {
        $data = $_POST;
        $result = Db::name('powers')->data($data)->insert();
        if ($result > 0) {
            return $this->success('添加成功', url('/adminPowerList'));
        } else {
            return $this->error('添加失败(ノ°ο°)ノ高能预警 ', url('/adminPowerList'));
        }
    }

    public function edit($id)
    {
        // 是否为AJAX方式请求
        if (!Request::instance()->isAjax()) {
            $this->error('您四不四迷路啦?!', '/');
        }
        // 查询指定用户信息
        $row = Db::name('powers')->field('pass', true)->find($id);
        if ($row === null) {
            $info['status'] = false;
            $info['id'] = $id;
            $info['datas'] = '查无数据,请重试..';
        } else {
            $info['status'] = true;
            $info['id'] = $row['id'];
            $info['power'] = $row['power'];
            $info['controller'] = $row['controller'];
            $info['action'] = $row['action'];
        }
        return json($info);
    }

    public function doedit($id)
    {
        $data = [
            'power' =>$_POST['power'],
            'controller' =>$_POST['controller'],
            'action' =>$_POST['action']
        ];
        $result = Db::name('powers')->where('id',$_POST['id'])->update($data);
        if ($result < 0) {
            return $this->error('编辑失败(ノ°ο°)ノ高能预警 ');
        } else {
            return $this->success('编辑成功', url('/adminPowerList'));
        }
    }

    public function delete($id)
    {
        $result = Db::name('powers')->delete($id);
        if ($result) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . '的用户删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . '的用户删除失败!';
        }

        return json($info);
    }
}
